package com.gitee.wsl.data.page

import com.gitee.wsl.lifecycle.nav.SelectAbleList
import kotlinx.serialization.Serializable

/**
 * Represents a state of Child Pages navigation model.
 *
 * @param items a list of child configurations, must be unique, can be empty.
 * @param selectedIndex an index of the selected child configuration.
 * Must be within the range of [items] indices if [items] is not empty, otherwise can be any number.
 */
@Serializable
data class Pages<C>(
    override val items: List<C>,
    override val selectedIndex: Int,
): SelectAbleList<C,Pages<C>> {
    /**
     * Creates empty [Pages].
     */
    constructor() : this(items = emptyList(), selectedIndex = -1)

    override fun copyOf(
        items: List<C>,
        selectedIndex: Int,
    ) = copy(items = items,selectedIndex=selectedIndex)

    init {
        if (items.isNotEmpty()) {
            require(selectedIndex in items.indices) {
                "The selectedIndex argument must be with the range: ${items.indices}. Actual: $selectedIndex."
            }
        }
    }
}
